#coding:utf-8

from django_jinja import library


lib = library.Library()


@lib.filter
def pagination(current_page, total_page, base_link):
    #显示8个分页链接
    page_view = 7
    page_html = ''

    if current_page > total_page:
        current_page = total_page

    start_page = 1 if (current_page - int(page_view / 2)) < 1 else (current_page - int(page_view / 2))
    end_page = start_page + page_view

    if end_page > total_page:
        end_page = total_page

    #显示上一页
    if current_page > 1:
        page_html += '<a href="%s/%s">←Prev</a>' % (base_link, (current_page - 1))

    for i in range(start_page, end_page):
        if i == current_page:
            page_html += '<span>%s</span>' % i
        else:
            page_html += '<a href="%s/%s">%s</a>' % (base_link, i, i)

    #显示下一页
    if current_page < total_page:
        page_html += '<a href="%s/%s">Next→</a>' % (base_link, (current_page + 1))

    return page_html